using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Framework.Library.Core.Utils.Miscellaneous;

namespace Framework.Library.Core.Utils.Collections
{
    public class ListWrapper<T> : IList<T>, IList
    {
        private readonly IList<T> _genericList;
        private readonly IList _list;
        private object _syncRoot;

        public ListWrapper(IList list)
        {
            ValidationUtils.ArgumentNotNull(list, "list");

            _list = list;
        }

        public ListWrapper(IList<T> list)
        {
            ValidationUtils.ArgumentNotNull(list, "list");

            _genericList = list;
        }

        #region IList Members

        int IList.Add(object value)
        {
            VerifyValueType(value);
            Add((T) value);

            return (Count - 1);
        }

        bool IList.Contains(object value)
        {
            if (IsCompatibleObject(value))
                return Contains((T) value);

            return false;
        }

        int IList.IndexOf(object value)
        {
            if (IsCompatibleObject(value))
                return IndexOf((T) value);

            return -1;
        }

        void IList.Insert(int index, object value)
        {
            VerifyValueType(value);
            Insert(index, (T) value);
        }

        bool IList.IsFixedSize
        {
            get { return false; }
        }

        void IList.Remove(object value)
        {
            if (IsCompatibleObject(value))
                Remove((T) value);
        }

        object IList.this[int index]
        {
            get { return this[index]; }
            set
            {
                VerifyValueType(value);
                this[index] = (T) value;
            }
        }

        void ICollection.CopyTo(Array array, int arrayIndex)
        {
            CopyTo((T[]) array, arrayIndex);
        }

        bool ICollection.IsSynchronized
        {
            get { return false; }
        }

        object ICollection.SyncRoot
        {
            get
            {
                if (_syncRoot == null)
                    Interlocked.CompareExchange(ref _syncRoot, new object(), null);

                return _syncRoot;
            }
        }

        #endregion

        #region IList<T> Members

        public int IndexOf(T item)
        {
            if (_genericList != null)
                return _genericList.IndexOf(item);

            return _list.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            if (_genericList != null)
                _genericList.Insert(index, item);

            _list.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            if (_genericList != null)
                _genericList.RemoveAt(index);

            _list.RemoveAt(index);
        }

        public T this[int index]
        {
            get
            {
                if (_genericList != null)
                    return _genericList[index];

                return (T) _list[index];
            }
            set
            {
                if (_genericList != null)
                    _genericList[index] = value;

                _list[index] = value;
            }
        }

        public void Add(T item)
        {
            if (_genericList != null)
                _genericList.Add(item);

            _list.Add(item);
        }

        public void Clear()
        {
            if (_genericList != null)
                _genericList.Clear();

            _list.Clear();
        }

        public bool Contains(T item)
        {
            if (_genericList != null)
                return _genericList.Contains(item);

            return _list.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            if (_genericList != null)
                _genericList.CopyTo(array, arrayIndex);

            _list.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get
            {
                if (_genericList != null)
                    return _genericList.Count;

                return _list.Count;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                if (_genericList != null)
                    return _genericList.IsReadOnly;

                return _list.IsReadOnly;
            }
        }

        public bool Remove(T item)
        {
            if (_genericList != null)
                return _genericList.Remove(item);

            bool contains = _list.Contains(item);

            if (contains)
                _list.Remove(item);

            return contains;
        }

        public IEnumerator<T> GetEnumerator()
        {
            if (_genericList != null)
                return _genericList.GetEnumerator();

            return new EnumeratorWrapper<T>(_list.GetEnumerator());
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            if (_genericList != null)
                return _genericList.GetEnumerator();

            return _list.GetEnumerator();
        }

        #endregion

        private static void VerifyValueType(object value)
        {
            if (!IsCompatibleObject(value))
                throw new ArgumentException(
                    string.Format(
                        "The value '{0}' is not of type '{1}' and cannot be used in this generic collection.", value,
                        typeof (T)), "value");
        }

        private static bool IsCompatibleObject(object value)
        {
            if (!(value is T) && (value != null || typeof (T).IsValueType))
                return false;

            return true;
        }
    }
}